package com.lidong.consumer;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建服务的消费者
 */
@RestController
public class ConsumerController {

   @Autowired
   HelloService helloService;

    @HystrixCommand(fallbackMethod = "testError",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),//时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")//失败率达到多少后断路
    })
    @RequestMapping(value = "/sayHello")
    public String sayHello(@RequestParam(value = "name",required = false) String name) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("name",name);
        String callServiceResult = helloService.sayHello(name);
        return callServiceResult;
    }

    public String testError(String dd) {
        System.out.println("超时了");
        return "超时了";
    }
}
